<?php
include_once 'conn.php';
$token = $_POST['token']; //获取token
$id = $_POST['id']; //获取商品id
$arr = [];
$sql = $db -> query("SELECT user_token FROM user WHERE user_token = '$token'");
if($sql -> num_rows > 0){
    // 是否存在一条记录，用户id、商品id与当前要存入的相同
    $res=$db->query("SELECT car_id FROM car WHERE goods_id = '$id'
                        AND user_id = (SELECT user_id FROM user WHERE user_token = '$token')");
    if ($res->num_rows > 0) {
        $db->query("UPDATE car SET car_goods_num=car_goods_num+1 WHERE
                    goods_id = '$id' AND user_id = (SELECT user_id FROM user WHERE user_token = '$token')");
    }else{
        $db -> query("INSERT INTO car (user_id,goods_id) VALUES
        ((SELECT user_id FROM user WHERE user_token = '$token'),'$id') ");
    }
}else{
    echo json_encode(['exist'=>false]);
}
$db -> close();
?>